﻿Imports System.IO
Imports System.Collections.Generic
Imports System.Windows.Forms

Public Class clsCompareFileInfo
    Implements IComparer
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
        Dim File1 As FileInfo
        Dim File2 As FileInfo

        File1 = DirectCast(x, FileInfo)
        File2 = DirectCast(y, FileInfo)

        Compare = DateTime.Compare(File2.LastWriteTime, File1.LastWriteTime)
    End Function
End Class

Public Class BackupRoller

#Region "Properties"
    Private _backupfile As String
    Public Property BackupFile() As String
        Get
            Return _backupfile
        End Get
        Set(ByVal value As String)
            _backupfile = value
        End Set
    End Property

    Private _backupdirectory As String
    Public Property BackupDirectory() As String
        Get
            Return _backupdirectory
        End Get
        Set(ByVal value As String)
            _backupdirectory = value
        End Set
    End Property

    Private _numberofbackups As String
    Public Property NumberOfBackups() As String
        Get
            Return _numberofbackups
        End Get
        Set(ByVal value As String)
            _numberofbackups = value
        End Set
    End Property

    Private _keepyesterday As Boolean
    Public Property KeepYesterday() As Boolean
        Get
            Return _keepyesterday
        End Get
        Set(ByVal value As Boolean)
            _keepyesterday = value
        End Set
    End Property

    Private _yesterdaynumber As Integer
    Public Property YesterdayNumber() As Integer
        Get
            Return _yesterdaynumber
        End Get
        Set(ByVal value As Integer)
            _yesterdaynumber = value
        End Set
    End Property

    Private _keepdays As List(Of DayOfWeek)
    Public Property KeepDays() As List(Of DayOfWeek)
        Get
            Return _keepdays
        End Get
        Set(ByVal value As List(Of DayOfWeek))
            _keepdays = value
        End Set
    End Property

    Private _keepdaysnumber As Integer
    Public Property KeepDaysNumber() As Integer
        Get
            Return _keepdaysnumber
        End Get
        Set(ByVal value As Integer)
            _keepdaysnumber = value
        End Set
    End Property

    Private _keepdates As List(Of Day)
    Public Property KeepDates() As List(Of Day)
        Get
            Return _keepdates
        End Get
        Set(ByVal value As List(Of Day))
            _keepdates = value
        End Set
    End Property

    Private _keepdatesnumber As Integer
    Public Property KeepDatesNumber() As Integer
        Get
            Return _keepdatesnumber
        End Get
        Set(ByVal value As Integer)
            _keepdatesnumber = value
        End Set
    End Property

    Private _keeptodaynumber As Integer
    Public Property KeepTodayNumber() As Integer
        Get
            Return _keeptodaynumber
        End Get
        Set(ByVal value As Integer)
            _keeptodaynumber = value
        End Set
    End Property

    Private _oldest As Integer
    Public Property Oldest() As Integer
        Get
            Return _oldest
        End Get
        Set(ByVal value As Integer)
            _oldest = value
        End Set
    End Property

    Private _likelihood As Integer
    Public Property Likelihood() As Integer
        Get
            Return _likelihood
        End Get
        Set(ByVal value As Integer)
            _likelihood = value
        End Set
    End Property

#End Region

    Public Function Initialise() As Boolean

        'db.Show()
        'db.echo("BackupRoller Initialised")

        Return True
    End Function


    Public Function BackupFolderExists(CreateIfNot As Boolean) As Boolean
        If Not (Directory.Exists(_backupdirectory)) Then
            If CreateIfNot Then
                Directory.CreateDirectory(_backupdirectory)
                Return False
            End If
        End If
        '  GameSetMatch.log.Log_Data("Backup Folder exists...")
        Return True
    End Function


    Private Function MonthDifference(ByVal first As DateTime, ByVal second As DateTime) As Integer
        Return Math.Abs((first.Month - second.Month) + 12 * (first.Year - second.Year))
    End Function

    Public Function Backup() As Boolean
        Dim dirinfo As DirectoryInfo
        Dim allFiles() As FileInfo

        Dim KeepFiles As New List(Of FileInfo)

        Dim daykeptcounter As Integer = 0
        Dim datekeptcounter As Integer = 0
        Dim keeptodaycoutner As Integer = 0
        Dim yesterdaycoutner As Integer = 0

        Dim daysurplusdate As Date = Nothing
        Dim datesurplusdate As Date = Nothing

        ' Dim 

        'check BU Folder exists
        BackupFolderExists(True)

        'Get existing files
        dirinfo = New DirectoryInfo(_backupdirectory)
        allFiles = dirinfo.GetFiles("*.*")
        Array.Sort(allFiles, New clsCompareFileInfo)

        '    GameSetMatch.log.Log_Data("")
        '   GameSetMatch.log.Log_Data("Folder Files:")
        For Each fl As FileInfo In allFiles
            ' GameSetMatch.log.Log_Data(fl.LastWriteTime.ToString("ddd d MMM") & ":" & fl.FullName.ToString())

            If MonthDifference(DateTime.Now, fl.LastWriteTime) > _oldest Then
                File.Delete(fl.FullName)
            End If

            If fl.LastWriteTime.Date = DateTime.Now.Date Then
                If keeptodaycoutner < _keeptodaynumber Then
                    KeepFiles.Add(fl)
                    keeptodaycoutner += 1
                Else
                    File.Delete(fl.FullName)
                End If

            ElseIf _keepyesterday = True And fl.LastWriteTime.Date = Now.AddDays(-1) Then
                If yesterdaycoutner < _keeptodaynumber Then
                    KeepFiles.Add(fl)
                    yesterdaycoutner += 1
                Else
                    File.Delete(fl.FullName)
                End If

            ElseIf KeepDays.Contains(fl.LastWriteTime.DayOfWeek) Then
                ' GameSetMatch.log.Log_Data("DayofWeek Match")
                If daykeptcounter < _keepdaysnumber Then
                    KeepFiles.Add(fl)
                    daykeptcounter += 1
                    daysurplusdate = fl.LastWriteTime.Date
                Else
                    If daysurplusdate = fl.LastWriteTime.Date Then
                        File.Delete(fl.FullName)
                    Else
                        KeepFiles.Add(fl)
                        daykeptcounter = 1
                    End If
                End If

            ElseIf KeepDates.Contains(fl.LastWriteTime.Day) Then

                If datekeptcounter < _keepdatesnumber Then
                    KeepFiles.Add(fl)
                    datekeptcounter += 1
                    datesurplusdate = fl.LastWriteTime.Date
                Else
                    If datesurplusdate = fl.LastWriteTime.Date Then
                        File.Delete(fl.FullName)
                    Else
                        KeepFiles.Add(fl)
                        datekeptcounter = 1
                    End If

                End If
            Else
                File.Delete(fl.FullName)
            End If

        Next

        '    GameSetMatch.log.Log_Data("")
        '    GameSetMatch.log.Log_Data("Kept Files:")
        'output keeps:
        For Each fl As FileInfo In KeepFiles
            '     GameSetMatch.log.Log_Data(fl.LastWriteTime.ToString("ddd d MMM") & ":" & fl.FullName.ToString())
        Next

        If File.Exists(_backupfile) Then
            File.Copy(_backupfile, _backupdirectory & "\" &
                      Path.GetFileNameWithoutExtension(_backupfile) &
                      DateTime.Now.ToString("_yyMMdd_HH-mm-ss") &
                      Path.GetExtension(_backupfile))
        Else
            Return False
        End If

        Return True

    End Function



End Class


